/**
 * 实时消息
 */

var $table = $('#ProductInfoListTable');

var replyMsg = function(id) {
	
	$.ajax({
		url : 'getFansMsg',
		type : 'post',
		data : {
			id : id
		},
		success : function(response) {
			if(response.data != null){

				$('#replyModal').modal('show');
				
				var fanMessage = response.data;
				var htmlStr = [];
                htmlStr.push("<div id=\"divFanMessage\" class=\"chat-message chat-primary\">");
                htmlStr.push("<div class=\"chat-contact\"><img src=\"" + fanMessage.headImgUrl + "\" class=\"head-img\" alt=\"\"></div>");
                htmlStr.push("<div class=\"chat-text\"><p>" + fanMessage.content + "</p></div></div>")
                var replyMessage = fanMessage.msgReplyList;
                if (replyMessage.length > 0) {
                    for (var i = 0; i < replyMessage.length; i++) {
                        htmlStr.push("<div class=\"chat-message me\"><div class=\"chat-contact\"></div>");
                        htmlStr.push("<div class=\"chat-text\"><p>" + replyMessage[i].content + "</p></div></div>");
                    }
                }
                
                $("#replyModal #replyDiv").html(htmlStr.join(""));
                $("#replyModal #replyH4").text("与" + fanMessage.nickName + "的实时消息");
                $("#formReply input[type='hidden'][name='fansMsgId']").val(fanMessage.id);
			}
			
		},
		error : function(result) {
			_showInfoMessage("操作失败。", 'error');
		}
	});
}

 
var config = {
	".chosen-select" : {},
	".chosen-select-deselect" : {
		allow_single_deselect : !0
	},
	".chosen-select-no-single" : {
		disable_search_threshold : 10
	},
	".chosen-select-no-results" : {
		no_results_text : "Oops, nothing found!"
	},
	".chosen-select-width" : {
		width : "95%"
	}
};

$(document).ready(function() {
	var _submiting1 = false;
	$('#formReply').validator({
		rules : {},
		fields : {
			'#content' : 'required'
		},
		valid : function(form) {
			if (_submiting1) {

				return;
			}
			_submiting1 = true;

			FormSubmit(form, function(response) {
				
				if(response.errorCode =='0') {
					_showInfoMessage("发送成功。", 'success');
					 
					var htmlStr= [];
	                htmlStr.push("<div class=\"chat-message me\"><div class=\"chat-contact\"></div>");
	                htmlStr.push("<div class=\"chat-text\"><p>" + response.data.content + "</p></div></div>");
	                $("#replyModal #replyDiv").append(htmlStr.join(""));
	                $("#formReply input[name='content']").val('');
	                

	    			$table.bootstrapTable('refresh');
				} else {
					_showInfoMessage(response.errorMsg, 'error');
				}
                
				_submiting1 = false;
			});
		}
	});
	
	$('#btnReplyMsg').click(function(){
		

		$('#formReply').submit();
		return false;
	});

					// 将表单元素和table属性转成json
					queryParams = function(params) {
						var temp = {};
						temp.currentResult = params.offset;
						temp.showCount = params.limit;
						var formParam = $('#queryForm').serializeArray();
						for (var i = 0; i < formParam.length; i++) {
							if (formParam[i].value != '') {
								temp[formParam[i].name] = formParam[i].value;
							}
						}
						return temp;
					};

					 

					$table.bootstrapTable({
								url : ctx + "/weixin/pageFansMsg",
								pagination : !0,
								showRefresh : !0,
								showToggle : !0,
								showColumns : !0,
								iconSize : "outline",
								striped : true, // 是否显示行间隔色
								queryParams : queryParams,// 传递参数（*）
								sidePagination : "server", // 服务端处理分页
								pageSize : 10, // 每页的记录行数（*）
								pageList : [ 5, 10, 20, 50 ], // 可供选择的每页的行数（*）
								minimunCountColumns : 2, // 可供选择的每页的行数（*）
								// detailView: true,
								sortName : undefined,
								sortOrder : 'asc',
								sortStable : false,
								type : "post",
								toolbar : "#tableEventsToolbar",
								contentType : "application/json; charset=utf-8",
								// detailFormatter : detailFormatter,
								// rowStyle : rowStyle,
								iconsPrefix : 'glyphicon', // glyphicon of fa
								// (font awesome)
								icons : {
									refresh : "glyphicon-repeat",
									toggle : "glyphicon-list-alt",
									columns : "glyphicon-list",
									detailOpen : 'glyphicon-plus icon-plus',
									detailClose : 'glyphicon-minus icon-minus',
									sortable : 'glyphicon-minus icon-minus'
								},
								onLoadSuccess : function(data) {
									if (data.total < 5) {
										$table.bootstrapTable('getOptions')["height"] = 400;
									} else {
										$table.bootstrapTable('getOptions')["height"] = '';
									}

									$table.bootstrapTable('hideColumn', 'id');
									$table.bootstrapTable('hideColumn', 'edit');
								},
								columns : [
										{
											title : '昵 称',
											field : 'nickName',
											align : 'center',
											valign : 'middle'
										},
										{
											title : '头 像',
											field : 'headImgUrl',
											align : 'center',
											valign : 'middle',
											formatter : function(value) {
												 
												return '<img src="'+value+'" class="head-img" />';
											}
										},
										{
											title : '消息内容',
											field : 'content',
											align : 'center',
											valign : 'middle'
										},
										{
											title : '时 间',
											field : 'createdTime',
											align : 'center',
											valign : 'middle'
										},
										{
											title : '回复状态',
											field : 'replyStatus',
											align : 'center',
											valign : 'middle',
											formatter : function(value) {
												 
												return value==0?'未回复':'已回复';
											}
										},
										{
											title : '操作',
											field : 'operate',
											align : 'center',
											width : '140',
											valign : 'middle',
											formatter : function(value, row,
													index) {
												/*return '<a onclick="replyMsg('
														+ row.id
														+ ')"><span class="glyphicon glyphicon-pencil" aria-hidden="true">回复</span></a>';*/
												
												 return '<a onclick="replyMsg('+ row.id+ ')"><button class="btn  btn-xs"  title="回复"><span  aria-hidden="true">回复</span></button></a>';

											}
										}]
							});
					for ( var selector in config)
						$(selector).chosen(config[selector]);
});